<?php

class Admin_EventController extends My_Controller_ActionAdmin {

    public function indexAction() {
        $daoEvent = new Application_Model_DAO_Event();
        $list = $daoEvent->getAllEvents()->query()->fetchAll();
        $this->view->list = $list;
    }

    public function saveAction() {
        $code = $this->_getParam('id');
        $isEdit = false;
        if (!empty($code)) {
            $daoEvent = new Application_Model_DAO_Event();
            $record = $daoEvent->getEventByColumn($code, 'code');
            $form = new Admin_Form_Event_Edit();
            if (!$this->_request->isPost()) {
                $record['id'] = $code;
                $form->populate($record);
            }
            $isEdit = true;
        } else {
            $form = new Admin_Form_Event_Add();
        }
        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();

        $daoEvent = new Application_Model_DAO_Event();
        if ($isEdit) {
            $record = $daoEvent->updateEvent($data, $code, 'code');
        } else {
            $record = $daoEvent->addEvent($data);
        }

        $this->addSuccess($this->translate('XXX_ADMIN_EVENT_ADD_SUCCESS'));
        $url = $this->url(array('controller' => 'event', 'action' => 'index'));
        $this->_redirect($url);
    }

    public function deleteAction() {
        $code = $this->_getParam('id');
        $form = new Admin_Form_Confirmation();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            if (!$this->_request->isPost()) {
                $form->getElement('id')->setValue($code);
            }
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        if (!$form->isConfirmed()) {
            return;
        }
        $code = $form->getElement('id')->getValue();
        $daoEvent = new Application_Model_DAO_Event();
        $daoEvent->deleteEventByColumn($code, 'code');

        $this->addSuccess($this->translate('XXX_ADMIN_EVENT_DELETE_SUCCESS'));
        $url = $this->url(array('controller' => 'event', 'action' => 'index'));
        $this->_redirect($url);
    }

    public function infoListAction() {
        $daoEvent = new Application_Model_DAO_Event();
        $list = $daoEvent->getAllInfos()->query()->fetchAll();
        $this->view->list = $list;
    }

    public function infoSaveAction() {
        $code = $this->_getParam('id');
        $isEdit = false;
        if (!empty($code)) {
            $daoEvent = new Application_Model_DAO_Event();
            $record = $daoEvent->getEventInfoByColumn($code, 'code');
            $form = new Admin_Form_Event_InfoEdit();
            if (!$this->_request->isPost()) {
                $record['id'] = $code;
                $form->populate($record);
            }
            $isEdit = true;
        } else {
            $form = new Admin_Form_Event_InfoAdd();
        }
        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();

        $daoEvent = new Application_Model_DAO_Event();
        if ($isEdit) {
            $record = $daoEvent->updateEventInfo($data, null, $code, 'code');
        } else {
            $record = $daoEvent->addEventInfo($data);
        }

        $this->addSuccess($this->translate('XXX_ADMIN_EVENT_INFO_ADD_SUCCESS'));
        $url = $this->url(array('controller' => 'event', 'action' => 'info-list'));
        $this->_redirect($url);
    }

    public function infoDeleteAction() {
        $code = $this->_getParam('id');
        $form = new Admin_Form_Confirmation();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            if (!$this->_request->isPost()) {
                $form->getElement('id')->setValue($code);
            }
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        if (!$form->isConfirmed()) {
            return;
        }
        $code = $form->getElement('id')->getValue();
        $daoEvent = new Application_Model_DAO_Event();
        $daoEvent->deleteEventInfoByColumn($code, 'code');

        $this->addSuccess($this->translate('XXX_ADMIN_EVENT_INFO_DELETE_SUCCESS'));
        $url = $this->url(array('controller' => 'event', 'action' => 'info-list'));
        $this->_redirect($url);
    }

    public function placeListAction() {
        $daoEvent = new Application_Model_DAO_Event();
        $list = $daoEvent->getAllPlaces()->query()->fetchAll();
        $this->view->list = $list;
    }

    public function placeSaveAction() {
        $code = $this->_getParam('id');
        $isEdit = false;
        if (!empty($code)) {
            $daoEvent = new Application_Model_DAO_Event();
            $record = $daoEvent->getEventPlaceByColumn($code, 'code');
            $form = new Admin_Form_Event_PlaceEdit();
            if (!$this->_request->isPost()) {
                $record['id'] = $code;
                $form->populate($record);
            }
            $isEdit = true;
        } else {
            $form = new Admin_Form_Event_PlaceAdd();
        }
        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();

        $daoEvent = new Application_Model_DAO_Event();
        if ($isEdit) {
            $record = $daoEvent->updateEventPlace($data, $code, 'code');
        } else {
            $record = $daoEvent->addEventPlace($data);
        }

        $this->addSuccess($this->translate('XXX_ADMIN_EVENT_PLACE_ADD_SUCCESS'));
        $url = $this->url(array('controller' => 'event', 'action' => 'place-list'));
        $this->_redirect($url);
    }

    public function placeDeleteAction() {
        $code = $this->_getParam('id');
        $form = new Admin_Form_Confirmation();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            if (!$this->_request->isPost()) {
                $form->getElement('id')->setValue($code);
            }
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        if (!$form->isConfirmed()) {
            return;
        }
        $code = $form->getElement('id')->getValue();
        $daoEvent = new Application_Model_DAO_Event();
        $daoEvent->deleteEventPlaceByColumn($code, 'code');

        $this->addSuccess($this->translate('XXX_ADMIN_EVENT_PLACE_DELETE_SUCCESS'));
        $url = $this->url(array('controller' => 'event', 'action' => 'place-list'));
        $this->_redirect($url);
    }

}

